﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Data;
using System.Data.SqlClient;
using PaperSystem.Models;

namespace PaperSystem.Models
{
    public class AdminRepository
    {
        PaperDatabaseEntities db;

        public AdminRepository()
        {
            db = new PaperDatabaseEntities();
        }

        //根据管理员id返回用户名
        public string AdminName(string id)
        {
            string username = "";
            try
            {
                int userId = Convert.ToInt32(id);
                var tempdata = db.Administrator.Where(a => a.Aid == userId).FirstOrDefault();
                username = tempdata.Aname;
            }
            catch (Exception e)
            {
                return username;
            }
            return username;
        }

        //查询所有教师与学生
        public List<UserList> GetList(QueryInfo info)
        {
            List<UserList> list1 = new List<UserList>();
            List<UserList> list2 = new List<UserList>();
            //若账号文本框为空
            if (info.user == null)
            {
                list1 = (from a in db.Teacher
                         select new UserList()
                         {
                             id = a.Tid,
                             name = a.Tname,
                             password = a.Tpassword,
                             role = "教师"
                         }).ToList();
                list2 = (from a in db.Student
                         select new UserList()
                         {
                             id = a.Sid,
                             name = a.Sname,
                             password = a.Spassword,
                             role = "学生"
                         }).ToList();
            }
            //若账号文本框不空
            else
            {
                list1 = (from a in db.Teacher
                         where a.Tid.Contains(info.user)
                         select new UserList()
                         {
                             id = a.Tid,
                             name = a.Tname,
                             password = a.Tpassword,
                             role = "教师"
                         }).ToList();
                list2 = (from a in db.Student
                         where a.Sid.Contains(info.user)
                         select new UserList()
                         {
                             id = a.Sid,
                             name = a.Sname,
                             password = a.Spassword,
                             role = "学生"
                         }).ToList();
            }
            //根据身份判断
            if (info.role == null)
            {
                list1.AddRange(list2);
                return list1;
            }
            else if (info.role == "教师")
            {
                return list1;
            }
            else if (info.role == "学生")
            {
                return list2;
            }
            return list1;
        }

        //创建下拉列表内容
        public SelectList CreateDropdownList()
        {
            List<SelectListItem> listItem = new List<SelectListItem>();
            listItem.Add(new SelectListItem { Text = "教师", Value = "教师" });
            listItem.Add(new SelectListItem { Text = "学生", Value = "学生" });
            return new SelectList(listItem, "Value", "Text", "");
        }

        //更新用户数据
        public bool UpdateUser(string id, string name, string password, string role)
        {
            try
            {
                if (name.Equals("") || password.Equals(""))
                {
                    return false;
                }
                if (role == "教师")
                {
                    var row = db.Teacher.Where(tt => tt.Tid == id).FirstOrDefault();
                    row.Tname = name;
                    row.Tpassword = password;
                    db.SaveChanges();
                }
                else if (role == "学生")
                {
                    var row = db.Student.Where(ss => ss.Sid == id).FirstOrDefault();
                    row.Sname = name;
                    row.Spassword = password;
                    db.SaveChanges();
                }
                else
                {
                }
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        public bool AddUser(string id, string name, string password, string role)
        {
            try
            {
                if (id.Equals("") || name.Equals("") || password.Equals(""))
                {
                    return false;
                }
                if (role == "教师")
                {
                    Teacher t = new Teacher();
                    t.Tid = id;
                    t.Tname = name;
                    t.Tpassword = password;
                    db.AddToTeacher(t);
                    db.SaveChanges();
                }
                else if (role == "学生")
                {
                    Student s = new Student();
                    s.Sid = id;
                    s.Sname = name;
                    s.Spassword = password;
                    db.AddToStudent(s);
                    db.SaveChanges();
                }
                else
                {
                }
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        public bool DeleteUsers(string deleteRows)
        {
            try
            {
                string[] users = deleteRows.Split(' ');
                string temp;
                foreach (string user in users)
                {
                    if (user.StartsWith("t"))
                    {
                        temp = user.Remove(0, 1);
                        var row = db.Teacher.Where(tt => tt.Tid == temp).FirstOrDefault();
                        db.DeleteObject(row);
                        db.SaveChanges();
                    }
                    else if (user.StartsWith("s"))
                    {
                        temp = user.Remove(0, 1);
                        var row = db.Student.Where(ss => ss.Sid == temp).FirstOrDefault();
                        db.DeleteObject(row);
                        db.SaveChanges();
                    }
                }
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }
    }
}
